#coding=utf-8
import os
import pickle

videoWidth   = 320      #监控视频宽度
videoHigh    = 240      #监控视频高度
area_y1      = 0.4375   #上面计数线百分比
area_y2      = 0.625    #下面计数线百分比
area_x1      = 0        #左边限制百分比
area_x2      = 1        #右边限制百分比
highlevel    = 1        #安装高度等级
moveMinLen   = 60       #两帧之间移动的最小位置

systemPhotoFilePath = '/run/photo.jpg'#系统拍照的文件存储目录
UseingFlaskVideo = True
debugMode = True

def setDeviceConfigParam(arg):
    global area_y1
    global area_y2
    global area_x1
    global area_x2
    global highlevel
    area_y1, area_y2, area_x1, area_x2, highlevel, aksphoto = arg

def setDeviceConfigParam(ay1,ay2,ax1,ax2,h):
   '''
   设置设备的全局设置参数
   :param ay1:
   :param ay2:
   :param ax1:
   :param ax2:
   :param h:
   :return:
   '''
   global area_y1
   global area_y2
   global area_x1
   global area_x2
   global highlevel

   area_y1 = ay1
   area_y2 = ay2
   area_x1 = ax1
   area_x2 = ax2
   highlevel = h

def getDeviceConfigParam_areay1():
   global area_y1
   return area_y1

def getDeviceConfigParam_areay2():
   global area_y2
   return area_y2

def getDeviceConfigParam_areax1():
   global area_x1
   return area_x1

def getDeviceConfigParam_areax2():
   global area_x2
   return area_x2

def getDeviceConfigParam_areax2():
   global highlevel
   return highlevel

class deviceConfig():
    '''
    设备参数类，负责管理系统的全局参数
    '''
    DevArgFileNameBat = "./DevArg.bat"
    DevArgFileName = "./DevArg.pkl"

    def __init__(self):
        #用于设备参数保存
        self.area_y1 = 0.4375
        self.area_y2 = 0.625
        self.area_x1 = 0
        self.area_x2 = 320
        self.highlevel = 1
        self.askphoto = False
        self.DevArgCRC = self.area_y1 + self.area_y2 + self.area_x1 + self.area_x2 + self.highlevel

    def setDevArgs(self,arg):
        '''
        设置设备工作参数，这是一个包含（y1,y2,x1,x2,high,photo)参数
        :param arg:
        :return:
        '''
        try:
            ay1, ay2, ax1, ax2, highlevel, askphoto = arg
            result = False
            if self.area_x1 != ax1 or self.area_x2 != ax2 or self.area_y1 != ay1 or self.area_y2 != ay2 or self.highlevel != highlevel:
                result = True
            self.area_y1 = ay1
            self.area_y2 = ay2
            self.area_x1 = ax1
            self.area_x2 = ax2
            self.highlevel = highlevel
            self.askphoto = askphoto
            self.DevArgCRC = self.area_y1 + self.area_y2 + self.area_x1 + self.area_x2 + self.highlevel
            return result
        except Exception,e:
            print "set device parameter exception",e
            return False

    def setDevArg(self,area_y1,area_y2,area_x1,area_x2,highlevel,askphoto = False):
        '''
        设置设备参数
        :param self:
        :param area_y1:
        :param area_y2:
        :param area_x1:
        :param area_x2:
        :param highlevel:
        :param askphoto:
        :return:
        '''
        self.area_y1 = area_y1
        self.area_y2 = area_y2
        self.area_x1 = area_x1
        self.area_x2 = area_x2
        self.highlevel = highlevel
        self.askphoto = askphoto
        self.DevArgCRC = self.area_y1 + self.area_y2 + self.area_x1 + self.area_x2 + self.highlevel

    def __makeDevArgCRC(self):
        self.DevArgCRC = self.area_y1 + self.area_y2 + self.area_x1 + self.area_x2 + self.highlevel

    def __getDevArgCRC(self,arg):
        print "读取参数",arg
        ay1,ay2,ax1,ax2,high,askphoto,check = arg
        crc = ay1 + ay2 + ax1 + ax2 + high#这个类型不同有问题。
        return crc

    def __checkDevArgCRC(self):
        try:
            tmpdata = set()
            if os.path.exists(self.DevArgFileName):#加载原始的系统参数
                print "尝试加载设备参数文件"
                f1 = open(self.DevArgFileName,'rb')
                tmpdata = pickle.load(f1)
                f1.close()

                print tmpdata
                (area_y1, area_y2, area_x1, area_x2, highlevel, askphoto, DevArgCRC) = tmpdata

                if self.__getDevArgCRC(tmpdata) == DevArgCRC:
                    print '加载加载设备参数ok'
                    self.area_y1 = area_y1
                    self.area_y2 = area_y2
                    self.area_x1 = area_x1
                    self.area_x2 = area_x2
                    self.highlevel = highlevel
                    self.askphoto = askphoto
                    self.DevArgCRC = DevArgCRC
                    print self.area_y1,self.area_y2,self.area_x1,self.area_x2,self.highlevel
                    return 0
                else:
                    print '加载设备参数crc校验失败'
                    os.remove(self.DevArgFileName)
                    return -1

            elif os.path.exists(self.DevArgFileNameBat):#如果没有系统参数文件加载备份文件
                print '尝试加载备份文件'
                f1 = open(self.DevArgFileNameBat,'rb')
                tmpdata = pickle.load(f1)
                f1.close()

                (ay1, ay2, ax1, ax2, high, askphoto, DevArgCRC) = tmpdata
                if self.__getDevArgCRC(tmpdata) == DevArgCRC:
                    self.area_y1 = ay1
                    self.area_y2 = ay2
                    self.area_x1 = ax1
                    self.area_x2 = ax2
                    self.highlevel = highlevel
                    self.DevArgCRC = DevArgCRC

                    shutil.copy(self.DevArgFileNameBat,self.DevArgFileName)
                else:
                    print "备份文件校验异常"
                    os.remove(self.DevArgFileNameBat)
                    return -1
            else:#新设备
                print "这个设备配置文件"
                return 1
        except Exception,e:
            print "加载设备参数异常"
            return -1

    def saveDevArg(self):
        '''
        @保存设备参数
        :return:
        '''
        try:
            self.__makeDevArgCRC()
            tmpdata = (self.area_y1,self.area_y2,self.area_x1,self.area_x2,self.highlevel,self.askphoto,self.DevArgCRC)

            #保存设备参数的备份文件
            f1 = open(self.DevArgFileNameBat,'wb')
            pickle.dump(tmpdata,f1,True)
            f1.close()

            #保存设备参数文件
            f2 = open(self.DevArgFileName,'wb')
            pickle.dump(tmpdata,f2,True)
            f2.close()

            return True

        except Exception,e:
            print '保存设备参数运行异常'
            return False

    def loadDevArg(self):
        '''
        @加载设备参数
        :return:
        '''
        try:
            print "开始加载设备参数"
            result = self.__checkDevArgCRC()
            print result
            if result == 0:
                return True
            elif result == -1:#加载设备参宿失败加载第二次为夹杂备份配置
                result = self.__checkDevArgCRC()
                if result == 0:
                    return True
                else:
                    return False
            elif result == 1:#新设备
                print 'This New Device'
                return True
        except Exception,e:
            print '加载设备参数运行异常'
            return False


